/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nweb_application.h"

#include <cstdlib>
#include <thread>
#include "cef/include/wrapper/cef_helpers.h"
#include "nweb_handler_delegate.h"

namespace OHOS::NWeb {
NWebApplication::NWebApplication(
    std::shared_ptr<NWebPreferenceDelegate> preference_delegate,
    std::string url,
    CefRefPtr<NWebHandlerDelegate> handler_delegate,
    void* window)
    : preference_delegate_(preference_delegate),
      url_(url),
      handler_delegate_(handler_delegate),
      window_(window) {}

NWebApplication::~NWebApplication() {}

std::string NWebApplication::GetURL() {
  return url_;
}

/* CefApp methods begin */
CefRefPtr<CefBrowserProcessHandler>
NWebApplication::GetBrowserProcessHandler() {
  return this;
}

CefRefPtr<CefRenderProcessHandler> NWebApplication::GetRenderProcessHandler() {
  return this;
}
/* CefApp methods end */

/* CefBrowserProcessHandler methods begin */
void NWebApplication::OnContextInitialized() {
  LOG(INFO) << "NWebApplication::OnContextInitialized";
  CEF_REQUIRE_UI_THREAD();
  CreateBrowser();
}
/* CefBrowserProcessHandler methods end */

/* CefRenderProcessHandler methods begin */
void NWebApplication::OnWebKitInitialized() {
  LOG(INFO) << "OnWebKitInitialized";
}
/* CefRenderProcessHandler methods begin */

CefRefPtr<CefClient> NWebApplication::GetDefaultClient() {
  // Called when a new browser window is created via the Chrome runtime UI
  return handler_delegate_;
}

void NWebApplication::PopulateCreateSettings(
    CefRefPtr<CefCommandLine> command_line,
    CefBrowserSettings& browser_settings) {
  if (command_line->HasSwitch(switches::kOffScreenFrameRate)) {
    browser_settings.windowless_frame_rate =
        atoi(command_line->GetSwitchValue(switches::kOffScreenFrameRate)
                 .ToString()
                 .c_str());
  }
}

void NWebApplication::CreateBrowser() {
  CefRefPtr<CefCommandLine> command_line =
      CefCommandLine::GetGlobalCommandLine();

  // Specify CEF browser settings here.
  CefBrowserSettings browser_settings;
  PopulateCreateSettings(command_line, browser_settings);
  browser_settings.background_color = 0xffffffff;
  preference_delegate_->ComputeBrowserSettings(browser_settings);

  if (command_line->HasSwitch(switches::kForTest)) {
    preference_delegate_->PutHasInternetPermission(true);
    preference_delegate_->PutBlockNetwork(false);
  }

  std::string url;
  url = command_line->GetSwitchValue(switches::kUrl);
  if (url.empty()) {
    url = GetURL();
  }

  // Information used when creating the native window.
  CefWindowInfo window_info;
  CefWindowHandle handle = kNullWindowHandle;
  window_info.SetAsWindowless(handle);

  // Create the first browser window.
  CefBrowserHost::CreateBrowser(window_info, handler_delegate_, url,
                                browser_settings, nullptr, nullptr);
  if (handler_delegate_ == nullptr) {
    return;
  }
  auto browser = handler_delegate_->GetBrowser();
  if (browser && browser->GetHost()) {
    browser->GetHost()->SetNativeWindow(window_);
  }
}
}  // namespace OHOS::NWeb